/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bff.spi.event.query;

import com.inspur.edp.bef.api.lcp.AuthInfo;
import com.inspur.edp.bff.api.exception.BffFrameworkException;
import com.inspur.edp.bff.api.exception.ErrorCodes;
import com.inspur.edp.bff.api.manager.IFSAction;
import com.inspur.edp.bff.api.manager.IFSManagerContext;
import com.inspur.edp.bff.api.manager.context.QueryContext;
import com.inspur.edp.cef.entity.condition.EntityFilter;
import com.inspur.edp.cef.entity.entity.IEntityData;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class BffQueryEventBroker {
    private static List<IBffQueryEventListener> eventLs = new ArrayList<IBffQueryEventListener>() {{
    }};

    private static IBffQueryEventListener getBffQueryEventListener(String className) {
        try {
            Class cls = Class.forName(className);
            IBffQueryEventListener listener = (IBffQueryEventListener) cls.newInstance();
            return listener;
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
            throw new BffFrameworkException(e, ErrorCodes.BFF_FRAMEWORK_2020);
        }

    }

    public static final void fireBeforeQuery(IFSManagerContext bffcontext, EntityFilter filter, AuthInfo authInfo) {
        for (IBffQueryEventListener el : eventLs) {
            el.beforeQuery(bffcontext, filter, authInfo);
        }
    }

    public static final void firebeforeTotalCXQAssembler(QueryContext querycontext) {
        for (IBffQueryEventListener el : eventLs) {
            el.beforeTotalCXQAssembler(querycontext);
        }
    }

    public static final void fireBeforeCXQAssembler(IFSAction beforeaction, QueryContext querycontext, IFSManagerContext bffcontext) {
        for (IBffQueryEventListener el : eventLs) {
            el.beforeCXQAssembler(beforeaction, querycontext, bffcontext);
        }
    }

    public static final void fireAfterCXQAssembler(IFSManagerContext bffcontext, QueryContext queryContext) {
        for (IBffQueryEventListener el : eventLs) {
            el.afterCXQAssembler(bffcontext, queryContext);
        }
    }

    public static final void fireafterTotalCXQAssembler(IFSManagerContext bffcontext, QueryContext querycontext, int TotalCount) {
        for (IBffQueryEventListener el : eventLs) {
            el.afterTotalCXQAssembler(bffcontext, querycontext, TotalCount);
        }
    }

    public static final void firebeforeTotalCoreQuery(QueryContext querycontext) {
        for (IBffQueryEventListener el : eventLs) {
            el.beforeTotalCoreQuery(querycontext);
        }
    }

    public static final void firebeforeWKZTotalCoreQuery(EntityFilter filter) {
        for (IBffQueryEventListener el : eventLs) {
            el.beforeWKZTotalCoreQuery(filter);
        }
    }

    public static final void fireafterWKZTotalCoreQuery(IFSManagerContext bffcontext, List<IEntityData> queryResult, List<IEntityData> vmdatas) {
        for (IBffQueryEventListener el : eventLs) {
            el.afterWKZTotalCoreQuery(bffcontext, queryResult, vmdatas);
        }
    }

    public static final void firebeforeCoreQuery(IFSAction queryaction, QueryContext querycontext, IFSManagerContext bffcontext) {
        for (IBffQueryEventListener el : eventLs) {
            el.beforeCoreQuery(queryaction, querycontext, bffcontext);
        }
    }

    public static final void fireprocessGetAuth(AuthInfo auth) {
        for (IBffQueryEventListener el : eventLs) {
            el.processGetAuth(auth);
        }
    }


    public static final void fireafterCoreQuery(IFSManagerContext context, HashMap<String, ArrayList<IEntityData>> queryResult, QueryContext queryContext) {
        for (IBffQueryEventListener el : eventLs) {
            el.afterCoreQuery(context, queryResult, queryContext);
        }
    }


    public static final void fireafterTotalCoreQuery(IFSManagerContext context, HashMap<String, ArrayList<IEntityData>> queryResult, QueryContext queryContext, int TotalCount) {
        for (IBffQueryEventListener el : eventLs) {
            el.afterTotalCoreQuery(context, queryResult, queryContext, TotalCount);
        }
    }

    public static final void firebeforeTotalCXHAssembler(QueryContext queryContext, IFSManagerContext bffcontext) {
        for (IBffQueryEventListener el : eventLs) {
            el.beforeTotalCXHAssembler(queryContext, bffcontext);
        }
    }


    public static final void firebeforeCXHAssembler(IFSAction cxhassembler, QueryContext queryContext, IFSManagerContext bffcontext) {
        for (IBffQueryEventListener el : eventLs) {
            el.beforeCXHAssembler(cxhassembler, queryContext, bffcontext);
        }
    }

    public static final void fireafterCXHAssembler(QueryContext queryContext, IFSManagerContext bffcontext) {
        for (IBffQueryEventListener el : eventLs) {
            el.afterCXHAssembler(queryContext, bffcontext);
        }
    }

    public static final void fireafterTotalCXHAssembler(QueryContext queryContext, IFSManagerContext bffcontext, int TotalCount) {
        for (IBffQueryEventListener el : eventLs) {
            el.afterTotalCXHAssembler(queryContext, bffcontext, TotalCount);
        }
    }

    public static final void fireAfterQuery(IFSManagerContext context, EntityFilter filter, List<IEntityData> result) {
        for (IBffQueryEventListener el : eventLs) {
            el.afterQuery(context, filter, result);
        }
    }

    public static final void fireQueryUnnormalStop(Exception e) {
        for (IBffQueryEventListener el : eventLs) {
            el.queryUnnormalStop(e);
        }
    }


}
